package com.silence.gmall.product.domain.repository.impl;

import com.baomidou.mybatisplus.extension.repository.CrudRepository;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.silence.gmall.product.assembler.BaseCategory1Factory;
import com.silence.gmall.product.domain.entity.BaseCategory1;
import com.silence.gmall.product.domain.entity.BaseCategory2;
import com.silence.gmall.product.domain.entity.BaseCategory3;
import com.silence.gmall.product.domain.mapper.BaseCategory1Mapper;
import com.silence.gmall.product.domain.repository.BaseCategory1Repository;
import com.silence.gmall.product.dto.BaseCategory1Dto;
import com.silence.gmall.web.dto.CategoryViewDto;
import com.silence.gmall.web.vo.res.CategoryResVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author silence
 * @Description 针对表【base_category1(一级分类表)】的数据库操作Repository实现
 * @Date 2025-08-17 16:28:16
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class BaseCategory1RepositoryImpl extends CrudRepository<BaseCategory1Mapper, BaseCategory1>
        implements BaseCategory1Repository {

    /**
     * 一级分类工厂
     */
    private final BaseCategory1Factory baseCategory1Factory;

    /**
     * 查询所有一级分类
     *
     * @return 一级分类列表
     */
    @Override
    public List<BaseCategory1Dto> listBaseCategory1() {
        return baseCategory1Factory.categoryList2CategoryResVoList(list());
    }

    /**
     * 数据库查询所有分类
     *
     * @return 所有分类树形结构
     */
    @Override
    public List<CategoryResVo> getCategoryTree() {
        MPJLambdaWrapper<BaseCategory1> wrapper = new MPJLambdaWrapper<BaseCategory1>()
                .selectAs(BaseCategory1::getId, CategoryResVo::getCategoryId)
                .selectAs(BaseCategory1::getName, CategoryResVo::getCategoryName)
                .selectCollection(BaseCategory2.class, CategoryResVo::getCategoryChild,category2 ->
                        category2.id(BaseCategory2::getId, CategoryResVo::getCategoryId)
                                .result(BaseCategory2::getName, CategoryResVo::getCategoryName)
                                .collection(BaseCategory3.class, CategoryResVo::getCategoryChild, category3 ->
                                        category3.id(BaseCategory3::getId, CategoryResVo::getCategoryId)
                                                .result(BaseCategory3::getName, CategoryResVo::getCategoryName)))
                .leftJoin(BaseCategory2.class, BaseCategory2::getCategory1Id, BaseCategory1::getId)
                .leftJoin(BaseCategory3.class, BaseCategory3::getCategory2Id, BaseCategory2::getId);
        return baseMapper.selectJoinList(CategoryResVo.class, wrapper);
    }

    /**
     * 查询分类信息视图
     *
     * @param category3Id 三级分类ID
     * @return 分类信息视图
     */
    @Override
    public CategoryViewDto getCategoryView(String category3Id) {
        MPJLambdaWrapper<BaseCategory1> wrapper = new MPJLambdaWrapper<BaseCategory1>()
                .selectAs(BaseCategory1::getId, CategoryViewDto::getCategory1Id)
                .selectAs(BaseCategory1::getName, CategoryViewDto::getCategory1Name)
                .selectAs(BaseCategory2::getId, CategoryViewDto::getCategory2Id)
                .selectAs(BaseCategory2::getName, CategoryViewDto::getCategory2Name)
                .selectAs(BaseCategory3::getId, CategoryViewDto::getCategory3Id)
                .selectAs(BaseCategory3::getName, CategoryViewDto::getCategory3Name)
                .leftJoin(BaseCategory2.class, BaseCategory2::getCategory1Id, BaseCategory1::getId)
                .leftJoin(BaseCategory3.class, BaseCategory3::getCategory2Id, BaseCategory2::getId)
                .eq(BaseCategory3::getId, category3Id);
        return baseMapper.selectJoinOne(CategoryViewDto.class, wrapper);
    }
}